<?php

namespace Home\Model;

class MatchTeamPlayerModel extends CommonModel {

    public function createMatchTeamPlayer($match_id){
        $this->mdlTeamPlayer = D('team_player');
        $homeTeamPlayers = $this->mdlTeamPlayer->findPlayerByTeamId($_POST['home_team_id']);
        $awayTeamPlayers = $this->mdlTeamPlayer->findPlayerByTeamId($_POST['away_team_id']);

        $dt = array();
        foreach ($homeTeamPlayers as $k => $v) {
            $dt[] = array(
                'match_id' => $match_id,
                'team_id' => $v['team_id'],
                'player_id' => $v['player_id'],
            );
        }
        foreach ($awayTeamPlayers as $k => $v) {
            $dt[] = array(
                'match_id' => $match_id,
                'team_id' => $v['team_id'],
                'player_id' => $v['player_id'],
            );
        }

        foreach ($dt as $k => $v) {
            $rs = $this->add($v);
        }

        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function join($flt){

        $dt['attendence'] = 1;
        $rs = $this->where($flt)->save($dt);

        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function findMatchTeamPlayerList($dt){
        $flt = array(
            'match_id' => $dt['match_id'],
            'team_id' => $dt['team_id'],
            'attendence' => 1,
        );
        $players = $this->where($flt)->select();

        $this->mdlPlayer = D('player');
        foreach ($players as $k => $v) {
            $p = $this->mdlPlayer->where(array('player_id'=>$v['player_id']))->find();
            $players[$k]['name'] = $p['name'];
            $players[$k]['type'] = $dt['type'];
            $players[$k]['oper'] = $dt['oper'];
        }

        return $players;
    }
}
